DESCRIPTION
DESCRIPTION Introduction SoX reads and writes audio files in most popular formats and can optionally apply effects to them. It can combine multiple input sources, synthesise audio, and, on many systems, act as a general purpose audio player or a multi-track audio recorder. It also has limited ability to split the input into multiple output files. All SoX functionality is available using just the sox command. To simplify playing and recording audio, if SoX is invoked as play, the output file is automatically set to be the default sound device, and if invoked as rec, the default sound device is used as an input source. Additionally, the soxi(1) command provides a convenient way to just query audio file header information. The heart of SoX is a library called libSoX. Those interested in extending SoX or using it in other programs should refer to the libSoX manual page: libsox(3). SoX is a command-line audio processing tool, particularly suited to making quick, simple edits and to batch processing. If you need an interactive, graphical audio editor, use audacity(1). The overall SoX processing chain can be summarised as follows: Input(s) -> Combiber -> Effects -> Output(s) Note however, that on the SoX command line, the positions of the Output(s) and the Effects are swapped w.r.t. the logical flow just shown. Note also that whilst options pertaining to files are placed before their respective file name, the opposite is true for effects. To show how this works in practice, here is a selection of examples of how SoX might be used. The simple sox recital.au recital.wav translates an audio file in Sun AU format to a Microsoft WAV file, whilst sox recital.au −b 16 recital.wav channels 1 rate 16k fade 3 norm performs the same format translation, but also applies four effects (down-mix to one channel, sample rate change, fade-in, nomalize), and stores the result at a bit-depth of 16. sox −r 16k −e signed −b 8 −c 1 voice-memo.raw voice-memo.wav converts ‘raw’ (a.k.a. ‘headerless’) audio to a self-describing file format, sox slow.aiff fixed.aiff speed 1.027 adjusts audio speed, sox short.wav long.wav longer.wav concatenates two audio files, and sox −m music.mp3 voice.wav mixed.flac mixes together two audio files. play "The Moonbeams/Greatest/*.ogg" bass +3 plays a collection of audio files whilst applying a bass boosting effect, play −n −c1 synth sin %−12 sin %−9 sin %−5 sin %−2 fade h 0.1 1 0.1 plays a synthesised ‘A minor seventh’ chord with a pipe-organ sound, rec −c 2 radio.aiff trim 0 30:00 records half an hour of stereo audio, and play −q take1.aiff & rec −M take1.aiff take1−dub.aiff (with POSIX shell and where supported by hardware) records a new track in a multi-track recording. Finally, rec −r 44100 −b 16 −s −p silence 1 0.50 0.1% 1 10:00 0.1% | \ sox −p song.ogg silence 1 0.50 0.1% 1 2.0 0.1% : \ newfile : restart records a stream of audio such as LP/cassette and splits in to multiple audio files at points with 2 seconds of silence. Also, it does not start recording until it detects audio is playing and stops after it sees 10 minutes of silence. N.B. The above is just an overview of SoX’s capabilities; detailed explanations of how to use all SoX parameters, file formats, and effects can be found below in this manual, in soxformat(7), and in soxi(1). File Format Types SoX can work with ‘self-describing’ and ‘raw’ audio files. ‘self-describing’ formats (e.g. WAV, FLAC, MP3) have a header that completely describes the signal and encoding attributes of the audio data that follows. ‘raw’ or ‘headerless’ formats do not contain this information, so the audio characteristics of these must be described on the SoX command line or inferred from those of the input file. The following four characteristics are used to describe the format of audio data such that it can be processed with SoX: sample rate The sample rate in samples per second (‘Hertz’ or ‘Hz’). Digital telephony traditionally uses a sample rate of 8000 Hz (8 kHz), though these days, 16 and even 32 kHz are becoming more common. Audio Compact Discs use 44100 Hz (44.1 kHz). Digital Audio Tape and many computer systems use 48 kHz. Professional audio systems often use 96 kHz. sample size The number of bits used to store each sample. Today, 16-bit is commonly used. 8-bit was popular in the early days of computer audio. 24-bit is used in the professional audio arena. Other sizes are also used. data encoding The way in which each audio sample is represented (or ‘encoded’). Some encodings have variants with different byte-orderings or bit-orderings. Some compress the audio data so that the stored audio data takes up less space (i.e. disk space or transmission bandwidth) than the other format parameters and the number of samples would imply. Commonly-used encoding types include floating-point, μ-law, ADPCM, signed-integer PCM, MP3, and FLAC. channels The number of audio channels contained in the file. One (‘mono’) and two (‘stereo’) are widely used. ‘Surround sound’ audio typically contains six or more channels. The term ‘bit-rate’ is a measure of the amount of storage occupied by an encoded audio signal over a unit of time. It can depend on all of the above and is typically denoted as a number of kilo-bits per second (kbps). An A-law telephony signal has a bit-rate of 64 kbps. MP3-encoded stereo music typically has a bit-rate of 128−196 kbps. FLAC-encoded stereo music typically has a bit-rate of 550−760 kbps. Most self-describing formats also allow textual ‘comments’ to be embedded in the file that can be used to describe the audio in some way, e.g. for music, the title, the author, etc. One important use of audio file comments is to convey ‘Replay Gain’ information. SoX supports applying Replay Gain information, but not generating it. Note that by default, SoX copies input file comments to output files that support comments, so output files may contain Replay Gain information if some was present in the input file. In this case, if anything other than a simple format conversion was performed then the output file Replay Gain information is likely to be incorrect and so should be recalculated using a tool that supports this (not SoX). The soxi(1) command can be used to display information from audio file headers. Determining & Setting The File Format There are several mechanisms available for SoX to use to determine or set the format characteristics of an audio file. Depending on the circumstances, individual characteristics may be determined or set using different mechanisms. To determine the format of an input file, SoX will use, in order of precedence and as given or available: To set the output file format, SoX will use, in order of precedence and as given or available: For all files, SoX will exit with an error if the file type cannot be determined. Command-line format options may need to be added or changed to resolve the problem. Playing & Recording Audio The play and rec commands are provided so that basic playing and recording is as simple as play existing-file.wav and rec new-file.wav These two commands are functionally equivalent to sox existing-file.wav −d and sox −d new-file.wav Of course, further options and effects (as described below) can be added to the commands in either form. Some systems provide more than one type of (SoX-compatible) audio driver, e.g. ALSA & OSS, or SUNAU & AO. Systems can also have more than one audio device (a.k.a. ‘sound card’). If more than one audio driver has been built-in to SoX, and the default selected by SoX when recording or playing is not the one that is wanted, then the AUDIODRIVER environment variable can be used to override the default. For example (on many systems): set AUDIODRIVER=oss play ... The AUDIODEV environment variable can be used to override the default audio device, e.g. set AUDIODEV=/dev/dsp2 play ... sox ... −t oss or set AUDIODEV=hw:soundwave,1,2 play ... sox ... −t alsa Note that the way of setting environment variables varies from system to system - for some specific examples, see ‘SOX_OPTS’ below. When playing a file with a sample rate that is not supported by the audio output device, SoX will automatically invoke the rate effect to perform the necessary sample rate conversion. For compatibility with old hardware, the default rate quality level is set to ‘low’. This can be changed by explicitly specifying the rate effect with a different quality level, e.g. play ... rate −m or by using the −−play−rate−arg option (see below). On some systems, SoX allows audio playback volume to be adjusted whilst using play. Where supported, this is achieved by tapping the ‘v’ & ‘V’ keys during playback. To help with setting a suitable recording level, SoX includes a peak-level meter which can be invoked (before making the actual recording) as follows: rec −n The recording level should be adjusted (using the system-provided mixer program, not SoX) so that the meter is at most occasionally full scale, and never ‘in the red’ (an exclamation mark is shown). See also −S below. Accuracy Many file formats that compress audio discard some of the audio signal information whilst doing so. Converting to such a format and then converting back again will not produce an exact copy of the original audio. This is the case for many formats used in telephony (e.g. A-law, GSM) where low signal bandwidth is more important than high audio fidelity, and for many formats used in portable music players (e.g. MP3, Vorbis) where adequate fidelity can be retained even with the large compression ratios that are needed to make portable players practical. Formats that discard audio signal information are called ‘lossy’. Formats that do not are called ‘lossless’. The term ‘quality’ is used as a measure of how closely the original audio signal can be reproduced when using a lossy format. Audio file conversion with SoX is lossless when it can be, i.e. when not using lossy compression, when not reducing the sampling rate or number of channels, and when the number of bits used in the destination format is not less than in the source format. E.g. converting from an 8-bit PCM format to a 16-bit PCM format is lossless but converting from an 8-bit PCM format to (8-bit) A-law isn’t. N.B. SoX converts all audio files to an internal uncompressed format before performing any audio processing. This means that manipulating a file that is stored in a lossy format can cause further losses in audio fidelity. E.g. with sox long.mp3 short.mp3 trim 10 SoX first decompresses the input MP3 file, then applies the trim effect, and finally creates the output MP3 file by re-compressing the audio - with a possible reduction in fidelity above that which occurred when the input file was created. Hence, if what is ultimately desired is lossily compressed audio, it is highly recommended to perform all audio processing using lossless file formats and then convert to the lossy format only at the final stage. N.B. Applying multiple effects with a single SoX invocation will, in general, produce more accurate results than those produced using multiple SoX invocations. Dithering Dithering is a technique used to maximise the dynamic range of audio stored at a particular bit-depth. Any distortion introduced by quantisation is decorrelated by adding a small amount of white noise to the signal. In most cases, SoX can determine whether the selected processing requires dither and will add it during output formatting if appropriate. Specifically, by default, SoX automatically adds TPDF dither when the output bit-depth is less than 24 and any of the following are true: For example, adjusting volume with vol 0.25 requires two additional bits in which to losslessly store its results (since 0.25 decimal equals 0.01 binary). So if the input file bit-depth is 16, then SoX’s internal representation will utilise 18 bits after processing this volume change. In order to store the output at the same depth as the input, dithering is used to remove the additional bits. Use the −V option to see what processing SoX has automatically added. The −D option may be given to override automatic dithering. To invoke dithering manually (e.g. to select a noise-shaping curve), see the dither effect. Clipping Clipping is distortion that occurs when an audio signal level (or ‘volume’) exceeds the range of the chosen representation. In most cases, clipping is undesirable and so should be corrected by adjusting the level prior to the point (in the processing chain) at which it occurs. In SoX, clipping could occur, as you might expect, when using the vol or gain effects to increase the audio volume. Clipping could also occur with many other effects, when converting one format to another, and even when simply playing the audio. Playing an audio file often involves resampling, and processing by analogue components can introduce a small DC offset and/or amplification, all of which can produce distortion if the audio signal level was initially too close to the clipping point. For these reasons, it is usual to make sure that an audio file’s signal level has some ‘headroom’, i.e. it does not exceed a particular level below the maximum possible level for the given representation. Some standards bodies recommend as much as 9dB headroom, but in most cases, 3dB (≈ 70% linear) is enough. Note that this wisdom seems to have been lost in modern music production; in fact, many CDs, MP3s, etc. are now mastered at levels above 0dBFS i.e. the audio is clipped as delivered. SoX’s stat and stats effects can assist in determining the signal level in an audio file. The gain or vol effect can be used to prevent clipping, e.g. sox dull.wav bright.wav gain −6 treble +6 guarantees that the treble boost will not clip. If clipping occurs at any point during processing, SoX will display a warning message to that effect. See also −G and the gain and norm effects. Input File Combining SoX’s input combiner can be configured (see OPTIONS below) to combine multiple files using any of the following methods: ‘concatenate’, ‘sequence’, ‘mix’, ‘mix-power’, ‘merge’, or ‘multiply’. The default method is ‘sequence’ for play, and ‘concatenate’ for rec and sox. For all methods other than ‘sequence’, multiple input files must have the same sampling rate. If necessary, separate SoX invocations can be used to make sampling rate adjustments prior to combining. If the ‘concatenate’ combining method is selected (usually, this will be by default) then the input files must also have the same number of channels. The audio from each input will be concatenated in the order given to form the output file. The ‘sequence’ combining method is selected automatically for play. It is similar to ‘concatenate’ in that the audio from each input file is sent serially to the output file. However, here the output file may be closed and reopened at the corresponding transition between input files. This may be just what is needed when sending different types of audio to an output device, but is not generally useful when the output is a normal file. If either the ‘mix’ or ‘mix-power’ combining method is selected then two or more input files must be given and will be mixed together to form the output file. The number of channels in each input file need not be the same, but SoX will issue a warning if they are not and some channels in the output file will not contain audio from every input file. A mixed audio file cannot be un-mixed without reference to the original input files. If the ‘merge’ combining method is selected then two or more input files must be given and will be merged together to form the output file. The number of channels in each input file need not be the same. A merged audio file comprises all of the channels from all of the input files. Un-merging is possible using multiple invocations of SoX with the remix effect. For example, two mono files could be merged to form one stereo file. The first and second mono files would become the left and right channels of the stereo file. The ‘multiply’ combining method multiplies the sample values of corresponding channels (treated as numbers in the interval −1 to +1). If the number of channels in the input files is not the same, the missing channels are considered to contain all zero. When combining input files, SoX applies any specified effects (including, for example, the vol volume adjustment effect) after the audio has been combined. However, it is often useful to be able to set the volume of (i.e. ‘balance’) the inputs individually, before combining takes place. For all combining methods, input file volume adjustments can be made manually using the −v option (below) which can be given for one or more input files. If it is given for only some of the input files then the others receive no volume adjustment. In some circumstances, automatic volume adjustments may be applied (see below). The −V option (below) can be used to show the input file volume adjustments that have been selected (either manually or automatically). There are some special considerations that need to made when mixing input files: Unlike the other methods, ‘mix’ combining has the potential to cause clipping in the combiner if no balancing is performed. In this case, if manual volume adjustments are not given, SoX will try to ensure that clipping does not occur by automatically adjusting the volume (amplitude) of each input signal by a factor of ¹/ n , where n is the number of input files. If this results in audio that is too quiet or otherwise unbalanced then the input file volumes can be set manually as described above. Using the norm effect on the mix is another alternative. If mixed audio seems loud enough at some points but too quiet in others then dynamic range compression should be applied to correct this - see the compand effect. With the ‘mix-power’ combine method, the mixed volume is approximately equal to that of one of the input signals. This is achieved by balancing using a factor of ¹/ √n instead of ¹/ n . Note that this balancing factor does not guarantee that clipping will not occur, but the number of clips will usually be low and the resultant distortion is generally imperceptible. Output Files SoX’s default behaviour is to take one or more input files and write them to a single output file. This behaviour can be changed by specifying the pseudo-effect ‘newfile’ within the effects list. SoX will then enter multiple output mode. In multiple output mode, a new file is created when the effects prior to the ‘newfile’ indicate they are done. The effects chain listed after ‘newfile’ is then started up and its output is saved to the new file. In multiple output mode, a unique number will automatically be appended to the end of all filenames. If the filename has an extension then the number is inserted before the extension. This behaviour can be customized by placing a %n anywhere in the filename where the number should be substituted. An optional number can be placed after the % to indicate a minimum fixed width for the number. Multiple output mode is not very useful unless an effect that will stop the effects chain early is specified before the ‘newfile’. If end of file is reached before the effects chain stops itself then no new file will be created as it would be empty. The following is an example of splitting the first 60 seconds of an input file into two 30 second files and ignoring the rest. sox song.wav ringtone%1n.wav trim 0 30 : newfile : trim 0 30 Stopping SoX Usually SoX will complete its processing and exit automatically once it has read all available audio data from the input files. If desired, it can be terminated earlier by sending an interrupt signal to the process (usually by pressing the keyboard interrupt key which is normally Ctrl-C). This is a natural requirement in some circumstances, e.g. when using SoX to make a recording. Note that when using SoX to play multiple files, Ctrl-C behaves slightly differently: pressing it once causes SoX to skip to the next file; pressing it twice in quick succession causes SoX to exit. Another option to stop processing early is to use an effect that has a time period or sample count to determine the stopping point. The trim effect is an example of this. Once all effects chains have stopped then SoX will also stop.